package top.leix.springdemo.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.leix.springdemo.Res;
import top.leix.springdemo.pojo.ApplyLabTeacher;
import top.leix.springdemo.pojo.ApplyRepairTeacher;
import top.leix.springdemo.services.ApplyRepairTeacherService;
import top.leix.springdemo.mapper.ApplyRepairTeacherMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 16435
* @description 针对表【apply_repair_teacher(报修表)】的数据库操作Service实现
* @createDate 2024-04-21 22:23:42
*/
@Service
public class ApplyRepairTeacherServiceImpl extends ServiceImpl<ApplyRepairTeacherMapper, ApplyRepairTeacher>
    implements ApplyRepairTeacherService{

    @Resource
    ApplyRepairTeacherMapper applyRepairTeacherMapper;
    /**
     * 老师提交维修申请
     * @param applyRepairTeacher
     * @return
     */
    @Override
    public Res addRepair(ApplyRepairTeacher applyRepairTeacher) {
        applyRepairTeacher.setRepairStatus("Pending");
        int insertCount = applyRepairTeacherMapper.insert(applyRepairTeacher);
        if (insertCount > 0) {
            return Res.ok(null);
        } else {
            return Res.failed("添加维修申请失败");
        }
    }
    /**
     * 更新维修申请
     * @param applyRepairTeacher
     * @return
     */
    @Override
    public Res updateRepairApplication(ApplyRepairTeacher applyRepairTeacher) {
        int updateCount = applyRepairTeacherMapper.updateById(applyRepairTeacher);
        if (updateCount > 0) {
            return Res.ok("更新申请成功");
        } else {
            return Res.failed("更新申请失败");
        }
    }
    /**
     * 根据账号查询维修申请
     * @param userAccount
     * @return
     */
    @Override
    public Res queryRepairApplication(String userAccount) {
        QueryWrapper<ApplyRepairTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applyUserAccount",userAccount);
        List<ApplyRepairTeacher> labList = applyRepairTeacherMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }

    /**
     * 老师查询未维修的维修申请
     * @param userAccount
     * @return
     */
    @Override
    public Res queryRepairPending(String userAccount) {
        QueryWrapper<ApplyRepairTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applyStatus","Pending");
        queryWrapper.eq("applyUserAccount",userAccount);
        List<ApplyRepairTeacher> labList = applyRepairTeacherMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }
    /**
     * 老师查询正在维修的维修申请
     * @param userAccount
     * @return
     */
    @Override
    public Res queryRepairRepairing(String userAccount) {
        QueryWrapper<ApplyRepairTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applyStatus","Repairing");
        queryWrapper.eq("applyUserAccount",userAccount);
        List<ApplyRepairTeacher> labList = applyRepairTeacherMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }
    /**
     * 老师查询完成维修的维修申请
     * @param userAccount
     * @return
     */
    @Override
    public Res queryRepairFinished(String userAccount) {
        QueryWrapper<ApplyRepairTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applyStatus","Finished");
        queryWrapper.eq("applyUserAccount",userAccount);
        List<ApplyRepairTeacher> labList = applyRepairTeacherMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }

    /**
     * 实验员查看自己的维修单
     * @param userName
     * @return
     */
    @Override
    public Res queryRepairByName(String userName) {
        List<ApplyRepairTeacher> list = applyRepairTeacherMapper.selectRepairByMonitor(userName);
        return Res.ok(list);
    }

    /**
     * 实验员更改状态为维修中
     * @return
     */
    @Override
    public Res updateRepairStatus(Integer applyID, String status) {
        int res = applyRepairTeacherMapper.updateStatus(applyID,status);
        if (res > 0){
            return Res.ok("OK");
        }
        return Res.failed(null);
    }

    @Override
    public Res addRepairNote(Integer applyID, String note) {
        int res = applyRepairTeacherMapper.addRepairNote(applyID,note);
        if (res > 0){
            return Res.ok("OK");
        }
        return Res.failed(null);
    }
}




